<?php
/**
 * Slipstream :: Highly adaptive dependency injection framework
 * Copyright 2010-2011 Ian Unruh (Xerof3ar)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace Slipstream\Extension;

use Slipstream\ContainerInterface;
use Slipstream\Activation\SessionInterface;
use Slipstream\Activation\SessionStage;
use Slipstream\Activation\Contributor\Construction\SimpleConstructionContributor;
use Slipstream\Activation\Contributor\Collection\MergeContributor;
use Slipstream\Activation\Contributor\Collection\RegistrationContributor;
use Slipstream\Activation\Contributor\Collection\WalkerCollectorContributor;
use Slipstream\Infrastructure\Context\Relevance\WeightComparator;
use Slipstream\Infrastructure\Parameter\ResolverService;
use Slipstream\Infrastructure\Parameter\PolicyResolver;
use Slipstream\Infrastructure\Parameter\TypehintResolver;
use Slipstream\Infrastructure\Parameter\MultiInjection\TypehintDetectionStrategy;
use Slipstream\Infrastructure\Parameter\Policy\ConstructorFilter;
use Slipstream\Configuration\ContributorPolicy;
use Slipstream\Configuration\Context\SortedWalker;

/**
 * Abstract extension that configures the minimum number of contributors
 * required to leverage the framework's core features
 *
 * @author Ian Unruh (Xerof3ar) <ianunruh@gmail.com>
 * @since Class avaliable since release 1.0
 */
abstract class AbstractCoreExtension extends AbstractExtension
{
	/**
	 * @var AnalyzerServiceInterface
	 */
	protected $relevanceAnalyzer;
	
	/**
	 * @var ScopeAnalyzerInterface
	 */
	protected $scopeAnalyzer;
	
	/**
	 * @var ResolverServiceInterface
	 */
	protected $typeResolver;
	
	/**
	 * Called when an extension is registered to the container
	 *
	 * @param ContainerInterface $container
	 * @return void
	 */
	public function onExtensionRegister(ContainerInterface $container)
	{
		$context = $container->getRootContext();
		
		$parameterResolver = new ResolverService();
		$parameterResolver->registerResolver(new PolicyResolver(new ConstructorFilter(), new TypehintDetectionStrategy()));
		$parameterResolver->registerResolver(new TypehintResolver());
		
		$contributor = new SimpleConstructionContributor();
		$contributor->setParameterResolver($parameterResolver);
		
		$policy = new ContributorPolicy();
		$policy->set(ContributorPolicy::OPTION_CONTRIBUTOR, $contributor);
		$policy->set(ContributorPolicy::OPTION_STAGE, SessionStage::Construction());
		
		$context->bind($policy);
	}
	
	/**
	 * Called when an activation session begins
	 *
	 * @param ContainerInterface $container
	 * @param SessionInterface $session
	 * @return void
	 */
	public function onSessionStart(ContainerInterface $container, SessionInterface $session)
	{
		$weightComparator = new WeightComparator($this->getRelevanceAnalyzer());
		$contextWalker = new SortedWalker($weightComparator);
		
		$registry = $session->getContributorRegistry();
		
		$registry->registerContributor(SessionStage::PreCollection(), new WalkerCollectorContributor($contextWalker, $this->getScopeAnalyzer()));
		$registry->registerContributor(SessionStage::PreCollection(), new MergeContributor());
		$registry->registerContributor(SessionStage::PreCollection(), new RegistrationContributor());
	}
	
	/**
	 * @return AnalyzerServiceInterface
	 */
	protected function getRelevanceAnalyzer()
	{
		if(is_null($this->relevanceAnalyzer)) {
			$this->relevanceAnalyzer = $this->createRelevanceAnalyzer();
		}
		return $this->relevanceAnalyzer;
	}
	
	/**
	 * @return ScopeAnalyzerInterface
	 */
	protected function getScopeAnalyzer()
	{
		if(is_null($this->scopeAnalyzer)) {
			$this->scopeAnalyzer = $this->createScopeAnalyzer();
		}
		return $this->scopeAnalyzer;
	}
	
	/**
	 * @return ResolverServiceInterface
	 */
	protected function getTypeResolver()
	{
		if(is_null($this->typeResolver)) {
			$this->typeResolver = $this->createTypeResolver();
		}
		return $this->typeResolver;
	}
	
	/**
	 * @return AnalyzerServiceInterface
	 */
	abstract protected function createRelevanceAnalyzer();
	
	/**
	 * @return ScopeAnalyzerInterface
	 */
	abstract protected function createScopeAnalyzer();
	
	/**
	 * @return ResolverServiceInterface
	 */
	abstract protected function createTypeResolver();
}